package blog.controller;

import java.time.LocalDateTime;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;

import blog.entity.Comment;
import blog.service.CommentService;
import blog.utils.IpUtil;

@Controller
@RequestMapping("/comment")
public class CommentController {
	@Resource
	private CommentService commentService;

	/**
	 * 显示全部回复
	 */
	@RequestMapping("")
	public String index(@RequestParam(required = false, defaultValue = "1") Integer pageIndex,
			@RequestParam(required = false, defaultValue = "10") Integer pageSize, ModelMap m) {
		PageInfo<Comment> commentPageInfo = commentService.getPageCommentList(pageIndex, pageSize);
		m.put("pageUrlPrefix", "comment?pageIndex"); // 把前缀数据传给分页页面

		m.put("pageInfo", commentPageInfo);
		return "Comment/comment-list";

	}

	/**
	 * 进入回复页面
	 */
	@RequestMapping("/reply/{commentId}")
	public String reply(@PathVariable("commentId") Integer commentId, ModelMap m) {
		Comment comment = commentService.getCommentById(commentId);
		m.put("comment", comment);
		return "Comment/comment-reply";

	}

	/**
	 * 进入回复跳转
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Comment comment, HttpServletRequest request) {
		comment.setCommentCreateTime(LocalDateTime.now());
		comment.setCommentIp(IpUtil.getIp(request));
		commentService.add(comment);
		return "forward:/comment";
	}
}
